import dateSelector from '@/components/afterSales/dateSelector.vue'
import recordCard from '../../components/afterSales/recordCard'
import uniSwipeAction from '../../components/uni-swipe-action/uni-swipe-action'
import defaultImg from '../../components/defaultImg/defaultImg'

export default {
  data() {
    return {
      selectedDate: '', //页面显示查询时间
      selectTime: '', //接口所需查询时间
      showsc: false,
      keyword: '',
      conunt: [0, 0],
      subCount: [199, 809],
      currentTab: 0, //一级选中tab 大项状态筛选 0、排队中 1、施工中 2、已取车 3、未消费离店
      subTab: [0, 0, 0, 0], //二级选中标签 小项筛选 0: 0、全部 1、洗车 2、轮保险 3、美容 4、钣喷 1: 0、全部 1、正常 2、暂停 3、完工未取车 2: 0、全部 3: 0、全部
      pageIndex: 1,
      total: 0,
      dataList: [],
      loadStatus: ['loadmore', 'loading', 'nomore'],
      loadStatusId: 0,
      typeScreen: 0, //二级tab,
      listCount: 0, //订单总数
      showImg: false,
    }
  },

  computed: {
    tabList() {
      return [
        {
          name: `${this.conunt[0]} \n 施工中`,
          list: [
            {
              name: '全部(' + this.listCount + ')',
            },
            {
              name: '正常',
            },
            {
              name: '暂停',
            },
            {
              name: '完工未取车',
            },
          ],
        },
        {
          name: `${this.conunt[1]}\n已取车(完工)`,
        },
      ]
    },
    isDisable() {
      return this.currentTab !== 0
    },
  },

  onLoad() {
    this.selectedDate = this.$u.timeFormat(new Date().getTime(), 'mm-dd')
  },

  onShow() {
    this.pageIndex = 1
    this.dataList = []
    this.getList()
  },

  methods: {
    //选择日期
    openSelector() {
      this.showsc = true
    },
    //选择到店记录
    confirmsc(data) {
      this.selectedDate = this.$u.timeFormat(data, 'mm-dd')
      this.selectTime = this.$u.timeFormat(data, 'yyyy-mm-dd')
      this.pageIndex = 1
      this.dataList = []
      this.getList()
      this.showsc = false
    },
    // 搜索
    onSearch(e) {
      console.log(e)
      this.keyword = e
      this.pageIndex = 1
      this.dataList = []
      this.getList()
    },
    closeTime() {
      this.showsc = false
    },

    //tab切换
    change(i) {
      this.currentTab = i
      this.pageIndex = 1
      this.typeScreen = 0
      ;(this.subTab = [0, 0, 0, 0]), (this.dataList = [])
      this.getList()
    },

    //二级tab切换
    changeSubTab(i) {
      // console.log(i)
      this.$set(this.subTab, this.currentTab, i)
      this.typeScreen = i
      this.dataList = []
      this.getList()
    },

    //获取列表
    getList() {
      uni.showLoading()
      this.loadStatusId = 1
      this.$api.afterSales
        .arrivalRecordListOfMy({
          pageIndex: this.pageIndex,
          pageSize: 10,
          selectTime: this.selectTime,
          searchKey: this.keyword,
          statusScreen: this.currentTab ? 2 : 1,
          typeScreen: this.typeScreen,
        })
        .then((res) => {
          this.conunt = [res.data.workCount, res.data.completeCount]
          this.listCount = res.data.workCount
          this.dataList = this.dataList.concat(res.data.list.list)
          // console.log(this.dataList)
          if (Math.ceil(this.listCount / 10) > this.pageIndex) {
            this.loadStatusId = 0
          } else {
            this.loadStatusId = 2
          }
          if (this.pageIndex === 1 && res.data.list.total === 0) {
            this.showImg = true
          }
          uni.hideLoading()
        })
        .catch((err) => {
          uni.hideLoading()
          console.log(err)
        })
    },
    // scroll-view到底部加载更多
    onreachBottom() {
      if (Math.ceil(this.listCount / 10) > this.pageIndex) {
        this.loadStatusId = 0
        this.pageIndex++
        this.getList()
      } else {
        this.loadStatusId = 2
      }
    },

    refresh() {
      this.dataList = []
      this.pageIndex = 1
      this.getList()
    },
  },

  components: {
    dateSelector,
    recordCard,
    uniSwipeAction,
    defaultImg,
  },
}
